<?php

namespace App\Http\Controllers\Backend;
use App\Common\ResponseCode;
use App\Http\Controllers\BaseController;
use App\Http\Middleware\AdminAuth;
use App\Params\Backend\DeliveryCompanyListParams;
use App\Params\Backend\DeliveryListParams;
use App\Params\Backend\DeliveryUpdateParam;
use App\Services\Sales\DeliveryService;
use App\Traits\VerifyRequestParam;

/**
 * 发货物流控制器
 *
 * @package App\Http\Controllers\H5
 * @author WangBin
 * @date 2021-06/11
 */
class DeliveryController extends BaseController
{
    use VerifyRequestParam;

    public function __construct()
    {
        $this->middleware(AdminAuth::class);
    }

    /**
     * 获取发货单初始化数据
     *
     * @return
     */
    public function init()
    {
        $ret = DeliveryService::getInstance()->getInit();
        return $this->success($ret);
    }

    /**
     * 保存发货单
     *
     * @return \Illuminate\Http\JsonResponse
     * @throws
     */
    public function update()
    {
        $param = DeliveryUpdateParam::create();
        // 创建发货单
        $ret = DeliveryService::getInstance()->update($param);
        if(!$ret) {
            return $this->fail(ResponseCode::DELIVERY_CREATE_FAIL);
        }
        return $this->success($ret);
    }

    /**
     * 获取物流公司列表
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function getCompanyList()
    {
        $param = DeliveryCompanyListParams::create();
        $company = DeliveryService::getInstance()->getCompanyList($param);
        return $this->success($company);
    }

    /**
     * 发货单列表
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function getList()
    {
        $param = DeliveryListParams::create();
        $deliveryList = DeliveryService::getInstance()->getList($param);
        return $this->success($deliveryList);
    }

    public function get()
    {
        $deliveryId = $this->verifyInteger('delivery_id');
        $delivery = DeliveryService::getInstance()->get($deliveryId);
        return $this->success($delivery);
    }
}
